package com.isoftstone.storage.events;

import java.util.ArrayList;
import java.util.List;

public final class TransferEventSupport {

	private final List<TransferListener> listeners = new ArrayList<TransferListener>();

	public synchronized void addTransferListener(final TransferListener listener) {
		if (listener != null) {
			listeners.add(listener);
		}
	}

	public synchronized void removeTransferListener(
			final TransferListener listener) {
		listeners.remove(listener);
	}

	public synchronized boolean hasTransferListener(
			final TransferListener listener) {
		return listeners.contains(listener);
	}

	public synchronized void fireTransferStarted(
			final TransferEvent transferEvent) {
		for (TransferListener listener : listeners) {
			listener.transferStarted(transferEvent);
		}
	}

	public synchronized void fireTransferProgress(
			final TransferEvent transferEvent, byte[] buffer, int length) {
		for (TransferListener listener : listeners) {
			listener.transferProgress(transferEvent, buffer, length);

		}
	}

	public synchronized void fireTransferCompleted(
			final TransferEvent transferEvent) {
		for (TransferListener listener : listeners) {
			listener.transferCompleted(transferEvent);

		}
	}

	public synchronized void fireTransferError(final TransferEvent transferEvent) {
		for (TransferListener listener : listeners) {
			listener.transferError(transferEvent);

		}
	}

	public synchronized void fireDebug(final String message) {

		for (TransferListener listener : listeners) {
			listener.debug(message);

		}
	}

	public synchronized void fireTransferInitiated(
			final TransferEvent transferEvent) {
		for (TransferListener listener : listeners) {
			listener.transferInitiated(transferEvent);
		}
	}
}
